import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

public class Test {
    public static void main(String[] args) {
        LinkedList<Integer> list=new LinkedList<>();
        list.add(1);//默认是尾插法
        list.add(2);
        list.add(3);
        System.out.println(list);

        for (Integer x:list
             ) {
            System.out.print(x+" ");
        }
        System.out.println();

        int size=list.size();
        System.out.println("_________--");//不要一边遍历一边删
        for (int i = 0; i <size ; i++) {//size会变
            System.out.print(list.get(i)+" ");
            //list.remove(i);
        }
        //利用迭代器遍历
        System.out.println("****Iterator***");
        Iterator<Integer>it =list.iterator();
        while(it.hasNext()){
            System.out.print(it.next()+" ");
        }
        System.out.println();
//        ListIterator和Iterator是父子关系，前者继承了后者
        //利用迭代器遍历listIterator
        System.out.println("****listIterator***");
        ListIterator<Integer>it2 =list.listIterator();
        while(it2.hasNext()){
            System.out.print(it2.next()+" ");
        }
        System.out.println();
        //利用迭代器遍历
        System.out.println("****ListIterator倒着打印***");
        ListIterator<Integer> it3 =list.listIterator(list.size());//只有ListIterator才有倒着打印的功能,父类Iterator没有
        while(it3.hasPrevious()){
            System.out.print(it3.previous()+" ");
        }
        System.out.println();
    }

}
